package com.tju.resource.oauth.controller.tb;

import com.tju.resource.annotation.Log;
import com.tju.resource.model.TbProcess;
import com.tju.resource.business.tb_process.TbProcessService;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.enum_utils.FunctionType;
import com.tju.resource.tool.tool.ClientParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 工序管理数据接口
 */
@RestController
public class TbProcessController {
    private static final String prefix = "process";
    @Autowired
    private TbProcessService tbProcessService;

    /**
     * 添加单工序管理信息
     *
     * @param tbProcess
     * @param result
     * @return
     */
    @PostMapping(prefix + "/addTbProcess.do")
    @PreAuthorize("hasAuthority('sys:craft:process:add')")
    @Log(type = FunctionType.Add)
    public Result<Object> addTbProcess(@Valid @RequestBody TbProcess tbProcess, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        } else
            return tbProcessService.addTbProcess(tbProcess);
    }

    /**
     * 修改单工序管理信息
     *
     * @param tbProcess
     * @param result
     * @return
     */
    @PostMapping(prefix + "/modifyTbProcess.do")
    @PreAuthorize("hasAuthority('sys:craft:process:edit')")
    @Log(type = FunctionType.Modify)
    public Result<Object> modifyTbProcess(@Valid @RequestBody TbProcess tbProcess, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        } else
            return tbProcessService.modifyTbProcess(tbProcess);
    }

    /**
     * 删除单工序管理信息
     *
     * @param tbProcess
     * @return
     */
    @Log(type = FunctionType.Delete)
    @PostMapping(prefix + "/deleteTbProcess.do")
    @PreAuthorize("hasAuthority('sys:craft:process:delete')")
    public Result<Object> deleteTbProcess(@RequestBody TbProcess tbProcess) {
        return tbProcessService.deleteTbProcess(tbProcess);
    }

    /**
     * 分页查询全部单工序管理信息
     *
     * @param parameter
     * @return
     */
    @Log(type = FunctionType.List)
    @PostMapping(prefix + "/getPageTbProcess.do")
    @PreAuthorize("hasAuthority('sys:craft:process:list')")
    public Result<Object> getPageTbProcess(@RequestBody ClientParameters parameter) {
        return tbProcessService.getPageTbProcess(parameter);
    }

    /**
     * 查询全部工序管理信息
     *
     * @return
     */
    @PostMapping(prefix + "/getTbProcess.do")
    public Result<Object> getTbProcess() {
        return tbProcessService.getTbProcess();
    }

    /**
     * 查询报工是否使用
     *
     * @param parameter
     * @return
     */
    @PostMapping(prefix + "/getReportedToUse.do")
    public Result<Object> getReportedToUse(@RequestBody ClientParameters parameter) {
        return tbProcessService.getReportedToUse(parameter.getThree());
    }

}
